package javabasic.threads.CreateThreadMethods;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableMethod {
    public static void main(String[] args) {
        //获取子线程的执行结果，需要借助futureTask
        MyThread3 myThread3 = new MyThread3();
        FutureTask<Integer> futureTask = new FutureTask<Integer>(myThread3);
        Thread thread = new Thread(futureTask);
        thread.start();
        while (!futureTask.isDone()){
            System.out.println("子线程未执行完毕，请等待。。。。");
        }
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
class MyThread3 implements Callable{
    private int sum;
    @Override
    public Object call() throws Exception {
        for (int i = 0; i < 10; i++) {
            Thread.sleep(10);
            sum+=i;
        }
        return sum;
    }
}